<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class HomesController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	public $name = 'Homes';

/**
 * Default helper
 *
 * @var array
 */
	
	public $helpers = array('Html', 'Session','Common');

/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array('Error','Image','News','NewsCategory');
	
	public $components=array('Email');

/**
 * Displays a view
 *
 * @param mixed What page to display
 * @return void
 */
	public function index() {
		$this->layout='default';		
		$this->News->locale=Configure::read('Config.language');
		$this->Image->locale=Configure::read('Config.language');
		$this->NewsCategory->locale=Configure::read('Config.language');
		//get image
		$images=$this->Image->find('all',array('conditions'=>'ImageCategory.published=1','order'=>'rand()','limit'=>4));
		//get home news:
		$news=$this->News->find('first',array('conditions'=>'News.published=1 and News.home=1 and News.important=1','order'=>'rand()'));
		if(empty($news))$news=$this->News->find('first',array('conditions'=>'News.published=1 and News.home=1','order'=>'rand()'));
		if(empty($news))$news=$this->News->find('first',array('conditions'=>'News.published=1','order'=>'rand()'));
		//get news controller:
				if(!empty($news)){
		$newscat=$this->NewsCategory->find('first',array('conditions'=>'NewsCategory.id='.$news['NewsCategory']['parent_id']));		$this->set('newsControllerName',$newscat['NewsCategory']['title']);
		}
		
		$this->set('css','home');
		$this->set('title_for_layout','Happy life tips');
		$this->set('images',$images);
		$this->set('news',$news);
		//$this->redirect("homes/home");
	}
	public function about(){
		$this->layout='default';
		$this->set('css','about');
		$this->set('title_for_layout','About Happy life tips');
		$rs='';
		$time=60;
		if(!empty($this->request->data) ){
			
			if(!empty($this->request->data['name']) && !empty($this->request->data['email']) && !empty($this->request->data['message'])){
				//if($this->data['Home']['sec']==$this->Session->read('Captcha')){
				if(!$this->Session->check('lastSendMail') ||  (time()- $this->Session->read('lastSendMail'))>$time){
					//Bat dau xu ly viec goi email
					
					$email_content = "Thông tin người gửi:<br /><br />";			
					$email_content .= "Người gửi: " .$this->request->data['name']."<br />";
					$email_content .= "Email: " .$this->request->data['email']."<br />";
					$email_content .= "Nội dung: " .$this->request->data['content']."<br />";
					
					
					$email = new CakeEmail();
					$email->from(array($this->request->data['email'] => $this->request->data['name']));
					$email->subject($this->request->data['subject']);				
					$email->emailFormat('html');
					$emails= explode(',',$this->webConfig['emailContact']);
					if(count($emails)>1){
						foreach($emails as $k=>$v){
							$email->to($v);
							$result = $email->send($email_content);	
						}				
					}
					else{
						$email->to($this->webConfig['emailContact']);
						$result = $email->send($email_content);
					}				
					
					if($result){
						$rs= "<script>alert('"._resource('Thanks for you contact!',true)."')</script>";
						$this->data=null;
					}
					else{
						$rs="<script>alert('"._resource('Send email error!',true)."')</script>";
					}
					$this->Session->write('lastSendMail',time());
				}else{
					$rs="<script>alert('"._resource('You must wait '.$time.' second to continue.',true)."')</script>";
				}
			}else{
				$rs="<script>alert('"._resource('Please fill all fields for contact form.',true)."')</script>";
			}
			
		}	
		$this->set('result',$rs);
		$this->render('about');	
		
	}
	
	function admin_index(){	
		$this->layout='admin_default';	
		if(empty($this->user['id'])){
			$this->layout=null;
			$this->render('admin_index1');
		}
		else $this->render('admin_index');
	}
	public function admin_home(){
		$this->render('admin_home');
	}
}
